package Practice;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MySQLCon {
	static final String uri = "jdbc:mysql://127.0.0.1:3306/test?useSSL=true";
	static final String user = "root";
	static final String password = "";
	
	public Connection getCon() {
		Connection con = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (Exception e) {} 
		try {
			con = DriverManager.getConnection(uri, user, password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return con;
	}
	
	public ResultSet query(String sql) {
		ResultSet rs = null;
		try {
			PreparedStatement pstmt = getCon().prepareStatement(sql);
			rs = pstmt.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return rs;
	}
	
	public void update(String sql) {
		try {
			PreparedStatement pstmt = getCon().prepareStatement(sql);
			pstmt.executeUpdate(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void Output(ResultSet rs) {
		try {
			while(rs.next()) {
				String number = rs.getString(1);
				String name = rs.getString(2);
				String birthday = rs.getString(3);
				float height = rs.getFloat(4);
				
				String message = number + " " + name 
								+ " " + " " + birthday + " " + " " + height;
				System.out.println(message);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void close() {
		try {
			getCon().close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
